package com.clkj.xiuzheng.controller;

import com.clkj.xiuzheng.pojo.Company;
import com.clkj.xiuzheng.pojo.Customer;
import com.clkj.xiuzheng.pojo.common.Result;
import com.clkj.xiuzheng.pojo.common.StatusCode;
import com.clkj.xiuzheng.service.CompanyService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping(value = "/company")
@Api(value = "/company", tags = {"公司管理"})
@CrossOrigin
public class CompanyController {

    @Autowired
   private CompanyService companyService;

    @ApiOperation(value = "添加公司信息")
    @PostMapping("/addCompany")
    public Result addCompany(@RequestBody Company company) {
        try {
            companyService.addCompany(company);
            return new Result(true, StatusCode.OK,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"添加失败");
        }

    }
    @ApiOperation(value = "修改公司状态为上、下线")
    @PostMapping("/updateCompanyDown")
    public Result updateCompanyDown(@RequestParam Integer companyId,Integer status) {
        try {
            companyService.updateCompanyDown(companyId,status);
            return new Result(true, StatusCode.OK,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"添加失败");
        }

    }
    @ApiOperation(value = "修改公司状态为上线")
    @PostMapping("/updateCompanyUp")
    public Result updateCompanyUp(@RequestParam Integer companyId) {
        try {
            companyService.updateCompanyUp(companyId);
            return new Result(true, StatusCode.OK,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"添加失败");
        }

    }
    @ApiOperation(value = "获取所有公司信息")
    @PostMapping("/listCompany")
    public Result listCompany(@RequestParam Integer pageSize,
                              @RequestParam Integer pageNum) {

        PageInfo pageInfo = companyService.listCompany(pageSize,pageNum);
        try {
            return new Result(true, StatusCode.OK,"查询成功",pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }

    @ApiOperation(value = "根据公司名称查询公司")
    @PostMapping("/getCompanyByName")
    public Result getCompanyByName(@RequestParam Integer pageSize,
                                     @RequestParam Integer pageNum,
                                     @RequestParam String  companyByName) {
        try {
            PageInfo pageInfo= companyService.getCompanyByName(pageSize, pageNum,companyByName);
            return new Result(true, StatusCode.OK, "查询成功", pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询失败");
        }
    }

    @ApiOperation(value = "公司登录")
    @PostMapping("/companyLogin")
    public Result companyLogin(String account,String password) {
        try {
            Map map =  companyService.companyLogin(account, password);
            return new Result(true, StatusCode.OK, "查询成功", map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询失败");
        }

    }


}
